package com.xh3140.android.widget.dialog;

import android.content.Context;

import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.Size;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.xh3140.android.widget.dialog.callback.ConfigParamsCallback;
import com.xh3140.android.widget.dialog.callback.ConfigViewCallback;
import com.xh3140.android.widget.dialog.params.BaseParams;
import com.xh3140.android.widget.dialog.params.ContentParams;
import com.xh3140.android.widget.dialog.params.DialogParams;
import com.xh3140.android.widget.dialog.view.ContentView;
import com.xh3140.android.widget.dialog.view.FooterView;
import com.xh3140.android.widget.dialog.view.HeaderView;
import com.xh3140.android.widget.dialog.view.RootView;

/**
 * 内容对话框
 */
@SuppressWarnings("unused")
public final class ContentDialog extends BaseDialog<ContentView> {
    /**
     * 内容对话框属性
     */
    private final Params mParams;

    /**
     * 内容对话框属性数据类
     */
    private static final class Params {
        public final DialogParams dialog;
        public final BaseParams<ContentDialog> base;
        public final ContentParams content;

        private Params(@NonNull DialogParams dialog, @NonNull BaseParams<ContentDialog> base, @NonNull ContentParams content) {
            this.dialog = dialog;
            this.base = base;
            this.content = content;
        }
    }

    private ContentDialog(@NonNull Params params) {
        super(params.dialog);
        mParams = params;
    }

    @NonNull
    @Override
    protected RootView<ContentView> initRootView(@NonNull Context context) {
        final ContentDialog dialog = this;
        return new RootView<ContentView>(context) {
            @Override
            protected HeaderView initHeaderView(@NonNull Context context) {
                return new HeaderView(context, mParams.base.header);
            }

            @Override
            protected ContentView initBodyView(@NonNull Context context) {
                return new ContentView(context, mParams.content);
            }

            @Override
            protected FooterView initFooterView(@NonNull Context context) {
                return new FooterView(context, dialog, mParams.base.footer);
            }
        };
    }

    /**
     * 获取对话框内容
     */
    public CharSequence getContentText() {
        return requireRootView().requireBodyView().getText();
    }

    /**
     * 内容对话框构建器
     * 设置内容对话框属性
     */
    public static final class Builder extends BaseDialog.Builder<ContentDialog, Builder> {
        /**
         * 对话框内容主体视图配置属性
         */
        private final ContentParams mContentParams;

        public Builder(@NonNull Context context, @IntRange(from = 0) int buttonCount) {
            super(context, buttonCount);
            mContentParams = new ContentParams(context);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull FragmentActivity activity, @IntRange(from = 0) int buttonCount) {
            super(activity, buttonCount);
            mContentParams = new ContentParams(mDialogParams.context);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull Fragment fragment, @IntRange(from = 0) int buttonCount) {
            super(fragment, buttonCount);
            mContentParams = new ContentParams(mDialogParams.context);
            mDialogParams.showDividers = -1;
        }

        /**
         * 构建对话框
         * 这个方法应该在所有配置结束后调用
         */
        @Override
        public ContentDialog build() {
            return new ContentDialog(new Params(mDialogParams, mBaseParams, mContentParams));
        }


        /**
         * 配置内容主体视图属性
         *
         * @param callback 配置回调
         */
        public Builder configContentParams(@NonNull ConfigParamsCallback<ContentParams> callback) {
            callback.callback(mContentParams);
            return this;
        }

        /**
         * 设置内容主体视图文本
         *
         * @param text 文本
         */
        public Builder setContentText(@Nullable CharSequence text) {
            mContentParams.text = text;
            return this;
        }

        /**
         * 设置内容主体视图文本字体大小 sp
         *
         * @param textSize 字体大小
         */
        public Builder setContentTextSize(float textSize) {
            mContentParams.textSize = textSize;
            return this;
        }

        /**
         * 设置内容主体视图文本字体颜色 argb
         *
         * @param textColor 字体颜色
         */
        public Builder setContentTextColor(@ColorInt int textColor) {
            mContentParams.textColor = textColor;
            return this;
        }

        /**
         * 设置内容主体视图文本字体样式
         *
         * @param typeface 字体样式
         */
        public Builder setContentTypeface(int typeface) {
            mContentParams.typeface = typeface;
            return this;
        }

        /**
         * 设置内容主体视图文本位置
         *
         * @param gravity 位置
         */
        public Builder setContentGravity(int gravity) {
            mContentParams.gravity = gravity;
            return this;
        }

        /**
         * 设置内容主体视图背景颜色 argb
         *
         * @param backgroundColor 背景颜色
         */
        public Builder setContentBackgroundColor(@ColorInt int backgroundColor) {
            mContentParams.backgroundColor = backgroundColor;
            return this;
        }

        /**
         * 设置内容主体视图内边距
         * 格式：[start, top, end, bottom] px
         *
         * @param padding 边距
         */
        public Builder setContentPadding(@Nullable @Size(4) int[] padding) {
            mContentParams.padding = padding;
            return this;
        }

        /**
         * 设置内容主体视图内边距
         *
         * @param padding 边距 px
         */
        public Builder setContentPadding(@Px int padding) {
            mContentParams.padding = new int[]{padding, padding, padding, padding};
            return this;
        }

        /**
         * 设置内容主体视图内边距
         *
         * @param left   左边距 px
         * @param top    上边距 px
         * @param right  右边距 px
         * @param bottom 下边距 px
         */
        public Builder setContentPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            mContentParams.padding = new int[]{left, top, right, bottom};
            return this;
        }

        /**
         * 设置内容主体视图外边距
         * 格式：[start, top, end, bottom] px
         *
         * @param margins 边距
         */
        public Builder setContentMargins(@Nullable @Size(4) int[] margins) {
            mContentParams.margins = margins;
            return this;
        }

        /**
         * 设置内容主体视图外边距
         *
         * @param margins 边距 px
         */
        public Builder setContentMargins(@Px int margins) {
            mContentParams.margins = new int[]{margins, margins, margins, margins};
            return this;
        }

        /**
         * 设置内容主体视图外边距
         *
         * @param left   左边距 px
         * @param top    上边距 px
         * @param right  右边距 px
         * @param bottom 下边距 px
         */
        public Builder setContentMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            mContentParams.margins = new int[]{left, top, right, bottom};
            return this;
        }

        /**
         * 设置内容主体视图高度 px
         *
         * @param height 高度
         */
        public Builder setContentHeight(@Px int height) {
            mContentParams.height = height;
            return this;
        }

        /**
         * 设置内容主体视图配置回调
         * 拥有绝对控制权的回调，它在其他属性配置结束后回调
         * 一般情况不要使用它，除非提供的现有配置属性不满足需求
         *
         * @param callback 配置回调
         */
        public Builder setContentViewCallback(@Nullable ConfigViewCallback<ContentView> callback) {
            mContentParams.callback = callback;
            return this;
        }
    }
}
